<?php
	class SystemPermissionsRowsController extends Controller
	{
		public $page_title = LANG_SYSTEM_PERMISSIONS_ROWS;
		public $layout = 'main';
		public $module = 'helpdesk';
		public $helpers = array ('html', 'htmlx','toolbars', 'grids|index');
		public $name = 'system_permissions_rows';
		public $use_logging = false;
		
		function edit($system_table_id, $row_id)
		{
			//checks whether to remember referer url or not
			if (!strstr(str_replace('_', '', $_SERVER[HTTP_REFERER]), 'systempermissionsrows')) 
			{
				$_SESSION[back_url]=$_SERVER[HTTP_REFERER];
			}
			
			$this->data[system_table_id] = $system_table_id;
			$this->data[row_id] = $row_id;
			$this->data[system_table] = $this->controller_model->Execute(array("system_table_id"=>$system_table_id),'sp_system_tables_get_by_id');	
			$this->data[permissions] = $this->controller_model->ListItems(array("system_table_id"=>$system_table_id, "row_id"=>$row_id));

			//saves permissions and redirects back
			if (!empty($this->request->post))
			{
				$this->savePermissions($this->request->post);
				$back_url = $_SESSION[back_url];
				unset($_SESSION[back_url]);
				header("Location: $back_url");
			}
			else
			{

			}	

		}
		
		function add($system_table_id, $row_id)
		{
			$this->data[system_table_id] = $system_table_id;
			$this->data[row_id] = $row_id;
			$this->data[roles] = $this->GetModelObject('roles')->ListItems();
					
			if (!empty($this->request->post))
			{
				if ($this->controller_model->Update($this->request->post))
				{
					$this->Redirect($this->name.'/edit/'.$this->data[system_table_id].'/'.$this->data[row_id]);
				}
				else
				{
				
				}
			}
			else
			{

			}	
		}
		
		function delete($id, $confirm = 0)
		{ 
			if ($confirm)
			{
				$this->controller_model->Delete($id);
				$redirect_url = $_SESSION[redirect_url];
				unset($_SESSION[redirect_url]);
				header("Location: ".$redirect_url);
			}
			else
			{
				$_SESSION[redirect_url] = $_SERVER[HTTP_REFERER];
				$this->layout = "empty";
				$this->data[$this->name][id] = $id;
			}
		}
		
		function savePermissions($data)
		{
			foreach ($data[permission] as $key=>$value)
			{
				$this->controller_model->Update($value);
			}
		}
		
		function view($system_table_id, $row_id)
		{
			$this->data[system_table_id] = $system_table_id;
			$this->data[row_id] = $row_id;
			$this->data[system_table] = $this->controller_model->Execute(array("system_table_id"=>$system_table_id),'sp_system_tables_get_by_id');	
			$this->data[permissions] = $this->controller_model->ListItems(array("system_table_id"=>$system_table_id, "row_id"=>$row_id));
		}	
	}
?>